package com.ruoyi.project.schedule.task.controller;


import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.schedule.task.domain.Task;
import com.ruoyi.project.schedule.task.service.TaskServiceImpl;
import com.ruoyi.project.system.user.domain.User;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 任务管理控制器
 */
@Controller
@RequestMapping("/schedule/task")
public class TaskController extends BaseController {

    private String prefix = "schedule/task";

    @Autowired
    private TaskServiceImpl taskService;

    /**
     * 显示任务管理页面
     * @return
     */
    @RequiresPermissions("schedule:task:view")
    @GetMapping
    public String ShowTask(){
        return prefix + "/task";
    }

    /**
     * 显示所有任务
     * @param task
     * @return
     */
    @RequiresPermissions("schedule:task:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Task task) {
        startPage();
        return getDataTable(taskService.selectTaskList(task));
    }

    /**
     * 显示添加业务页面
     * @return
     */
    @GetMapping("/add")
    public String ShowAdd() {
        return prefix + "/add";
    }

    /**
     * 添加任务
     * @param task
     * @return
     */
    @RequiresPermissions("schedule:task:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addTask(Task task) {
        return taskService.insertTask(task);
    }

    /**
     * 查询特定部门所有用户
     * @param user
     * @return
     */
    @GetMapping("/selectByDeptId")
    @ResponseBody
    public List<User> selectUser(User user){
        return taskService.selectUser(user);
    }

    /**
     * 移除任务
     * @param ids
     * @return
     */
    @RequiresPermissions("schedule:task:remove")
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        try
        {
            return toAjax(taskService.deleteTaskByIds(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }

    /**
     * 编辑查看任务
     * @param taskId
     * @param mmap
     * @return
     */
    @GetMapping("/edit/{taskId}")
    public String edit(@PathVariable("taskId") Long taskId, ModelMap mmap) {
        mmap.put("task", taskService.selectByTaskId(taskId));
        return prefix + "/edit";
    }

    /**
     * 完成任务
     * @param task
     * @return
     */
    @RequiresPermissions("schedule:task:update")
    @PostMapping("/update")
    @ResponseBody
    public AjaxResult Task(Task task){
        return taskService.updateTaskBy(task);
    }

}
